<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

	<title>Test Dijit Internal Event: "ondijitclick"</title>

	<script type="text/javascript" src="../../../dojo/dojo.js" data-dojo-config="isDebug: true, async: true"></script>
	<script type="text/javascript">
		require([
			"doh/runner", "dojo/robotx",
			"dojo/_base/array", "dojo/aspect", "dojo/dom", "dojo/keys", "dojo/on", "dojo/domReady!"
		], function(doh, robot, array, aspect, dom, keys, on){

			robot.initRobot('../_Widget-ondijitclick.html');

			// Event monitoring
			var widgetClicks = 0, buttonClicks = 0, button2Clicks = 0, thirdClicks = 0;
			var w;

			doh.register(function setup(){
				// get pointer to registry in the iframe
				registry = robot.window.require("dijit/registry");
			});

			doh.register("setup", function(){
				robot.sequence(function(){
					w = registry.byId("first");
					aspect.after(w, "_onClick", function(){
						widgetClicks++;
					}, true);
					on(dom.byId("plainbutton", robot.doc), "click", function(){
						buttonClicks++;
					});
					on(dom.byId("plainbutton2", robot.doc), "click", function(){
						button2Clicks++;
					});
					on(dom.byId("third", robot.doc), "click", function(){
						thirdClicks++;
					});
				});
			});

			// Test ondijitclick on a <div> which refocuses onto a native <button> node.
			// Make sure that the <button> doesn't get a spurious click event.
			array.forEach(["SPACE", "ENTER"], function(key){

				doh.register("ondijitclick by " + key, [
					{
						name: "ondijitclick by " + key,
						timeout: 5000,
						runTest: function(){
							widgetClicks = buttonClicks = 0;
							var d = new doh.Deferred();

							// Keyboard-click the widget
							robot.sequence(function(){
								w.domNode.focus();
							}, 500);
							robot.keyDown(keys[key], 250, {});
							robot.keyUp(keys[key], 250, {});

							// Check that ondijitclick fired but no spurious event
							// on the widget that got focused in the ondijitclick handler
							robot.sequence(d.getTestCallback(function(){
								doh.is(1, widgetClicks, "ondijitclick handler fired");
								doh.is(0, buttonClicks, "spurious button click event");
							}), 500);

							return d;
						}
					},
					{
						name: "ondijitclick by " + key + " w/modifier",
						timeout: 5000,
						runTest: function(){
							widgetClicks = buttonClicks = 0;
							var d = new doh.Deferred();

							robot.sequence(function(){
								w.domNode.focus();
							}, 500);
				
							// Try shift-space/shift-enter.
							// Don't try ctrl-space or meta-space since those keystrokes have special functions
							// (like context menu) in various browsers.

							var shift;
							aspect.after(w, "onClick", function(evt){
								shift = evt.shiftKey;
							}, true);

							robot.keyPress(keys[key], 100, {shift: true});

							robot.sequence(d.getTestCallback(function(){
								doh.is(1, widgetClicks, "ondijitclick handler fired");
								doh.t(shift, "shift flag was set");
								doh.is(0, buttonClicks, "button click handler wasn't fired");
							}), 500);

							return d;
						}
					}
				]);
			});

			// Test ondijitclick on a <div> which refocuses onto a native <textarea> node.
			// Tests that the keyboard event gets supressed so it doesn't modify the focused node.
			array.forEach(["SPACE","ENTER"], function(key){

				doh.register("focus to textarea by " + key, [
					{
						name: "ondijitclick by " + key,
						timeout: 5000,
						runTest: function(){
							var d = new doh.Deferred(),
								w = registry.byId("second");

							// Keyboard-click the widget
							robot.sequence(function(){
								w.domNode.focus();
							}, 500);
							robot.keyPress(keys[key], 500, {});

							// Check that ondijitclick fired but that focused textarea didn't get the key
							robot.sequence(d.getTestCallback(function(){
								doh.is("hello world", dom.byId("textarea", robot.doc).value);
							}), 500);

							return d;
						}
					}
				]);
			});

			// Test onclick on a <button> which refocuses onto a <div> w/an ondijitclick handler.
			// Make sure that the <div> doesn't get a spurious click event.
			array.forEach(["SPACE", "ENTER"], function(key){

				doh.register("focus to ondijitclick-div by " + key, [
					{
						name: "click by " + key,
						timeout: 5000,
						runTest: function(){
							button2Clicks = thirdClicks = 0;

							var d = new doh.Deferred(),
								button = dom.byId("plainbutton2", robot.doc);

							// Keyboard-click the native button
							robot.sequence(function(){
								button.focus();
							}, 500);
							robot.keyPress(keys[key], 500, {});

							// Check that ondijitclick didn't fire
							robot.sequence(d.getTestCallback(function(){
								doh.is(1, button2Clicks, "button2 was clicked (and focus moved to ondijitclick-div)");
								doh.is(0, thirdClicks, "ondijitclick-div didn't get a spurious click event");
							}), 500);

							return d;
						}
					}
				]);
			});

			// Test clicking on a <button> with an ondijitclick handler.
			// The main danger is getting two click events because the browser natively handles space-->click
			array.forEach(["SPACE", "ENTER"], function(key){

				doh.register("click ondijitclick button by " + key, [
					{
						name: "click by " + key,
						timeout: 5000,
						runTest: function(){

							var d = new doh.Deferred(),
								buttonWidget = registry.byId("widgetbutton"),
								buttonNode = dom.byId("widgetbutton", robot.doc);

							buttonWidget.clickCount = 0;

							// Keyboard-click the widget button
							robot.sequence(function(){
								buttonNode.focus();
							}, 500);
							robot.keyPress(keys[key], 500, {});

							// Check that ondijitclick fired exactly once
							robot.sequence(d.getTestCallback(function(){
								doh.is(1, buttonWidget.clickCount, "one click event");
							}), 500);

							return d;
						}
					}
				]);
			});

			doh.register("bubbling", [
				{
					name: "bubbling",
					timeout: 5000,
					runTest: function(){

						var d = new doh.Deferred(),
							outerDiv = dom.byId("outer", robot.doc),
							buttonNode = dom.byId("bubbleTestWidget", robot.doc),
							buttonWidget = registry.byId("bubbleTestWidget");

						// Listen for click events on the outer DOMNode.
						var clicks = 0;
						on(outerDiv, "click", function(){
							clicks++;
						});

						// Keyboard-click the widget button
						robot.sequence(function(){
							buttonNode.focus();
						}, 500);
						robot.keyPress(keys.ENTER, 500, {});

						// Check that one click event bubbled
						robot.sequence(d.getTestCallback(function(){
							doh.is(1, buttonWidget.clickCount, "one click event");
							doh.is(1, clicks, "one click event bubbled");
						}), 500);

						return d;
					}
				}
			]);

			doh.run();
		});

	</script>
</head>
</html>
